// 异步任务。假设它在实践中做了一些更有用的事情。
/**
 * @param {number | undefined} time
 * @param {number} value
 * @returns {Promise<number>}
 */
async function delayedValue(time, value) {
  console.time(`delayedValue?time=${time}`);
  return new Promise((resolve /* , reject*/) => {
    setTimeout(() => {
      console.log(`globals?time=${time}`, performance.now() - baseTime);
      resolve(value);
      console.timeEnd(`delayedValue?time=${time}`);
    }, time);
  });
}

/**
 * @returns {AsyncGenerator<number, void, undefined>}
 */
async function* generate() {
  yield await delayedValue(2000, 1);
  yield await delayedValue(100, 2);
  yield await delayedValue(500, 3);
  yield await delayedValue(250, 4);
  yield await delayedValue(125, 5);
  yield await delayedValue(50, 6);
  console.log("全部完成！");
}

/**
 * @returns {Promise<void>}
 */
async function main() {
  for await (const value of generate()) {
    console.log("值", value);
  }
}
const baseTime = performance.now();
main().catch((err) => console.error(err));
